//
//  UIScrollView+ExEmpty.m
//  MedLighter
//
//  Created by lijsoon on 16/4/21.
//  Copyright © 2016年 liwen. All rights reserved.
//

#import "UIScrollView+ExEmpty.h"
#import <objc/runtime.h>
#import "UIImage+Extension.h"

@implementation UIScrollView (ExEmpty)
static char EmptyContentKey;
- (void)setEmptyContent:(NSString *)content
{
    self.emptyDataSetDelegate = self;
    self.emptyDataSetSource = self;
    
    if (![content isEqualToString:self.tipContent]) {
        [self willChangeValueForKey:@"EmptyContent"];
        objc_setAssociatedObject(self, &EmptyContentKey,
                                 content,
                                 OBJC_ASSOCIATION_ASSIGN);
        [self didChangeValueForKey:@"EmptyContent"];
    }
}

- (NSString *)tipContent
{
    return objc_getAssociatedObject(self, &EmptyContentKey);
}

static char EmptyImageNameKey;
- (void)setEmptyImageName:(UIImage *)imageName
{
    if (imageName !=self.tipImg) {
        [self willChangeValueForKey:@"EmptyImageName"];
        objc_setAssociatedObject(self, &EmptyImageNameKey,
                                 imageName,
                                 OBJC_ASSOCIATION_ASSIGN);
        [self didChangeValueForKey:@"EmptyImageName"];
    }
}

- (UIImage *)tipImg{
     return objc_getAssociatedObject(self, &EmptyImageNameKey);
}
//- (NSString *)tipImageName
//{
//    return objc_getAssociatedObject(self, &EmptyImageNameKey);
//}

static char EmptyButtonNameKey;
static char EmptyButtonClickBlockKey;
- (void)setEmptyButtonContent:(NSString *)buttonTips handerClickButtonBlock:(ChooseButtonClickButton)chooseButtonClick;
{
    if (![buttonTips isEqualToString:self.buttonTips]) {
        [self willChangeValueForKey:@"EmptyButtonNameKey"];
        objc_setAssociatedObject(self, &EmptyButtonNameKey,
                                 buttonTips,
                                 OBJC_ASSOCIATION_ASSIGN);
        [self didChangeValueForKey:@"EmptyButtonNameKey"];
    }
    
    if (chooseButtonClick) {
        objc_setAssociatedObject(self, &EmptyButtonClickBlockKey,
                                 chooseButtonClick,
                                 OBJC_ASSOCIATION_COPY);
    }
}

- (NSString *)buttonTips
{
    return objc_getAssociatedObject(self, &EmptyButtonNameKey);
}

- (ButtonClickBlock)buttonClickBlock
{
    return objc_getAssociatedObject(self, &EmptyButtonClickBlockKey);
}

static char EmptyTopMarginKey;
- (void)setEmptyTopMargin:(NSNumber *)topMargin
{
    if (topMargin != self.topMargin) {
        [self willChangeValueForKey:@"EmptyTopMargin"];
        objc_setAssociatedObject(self, &EmptyTopMarginKey,
                                 topMargin,
                                 OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        [self didChangeValueForKey:@"EmptyTopMargin"];
    }
}

- (NSNumber *)topMargin
{
    return objc_getAssociatedObject(self, &EmptyTopMarginKey);
}

#pragma mark - DZNEmptyDataSetDelegate Method
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *text = self.tipContent;
    UIFont *font = font = [UIFont systemFontOfSize:14];
    UIColor *textColor = [UIColor grayColor];
    
    NSMutableDictionary *attributes = [NSMutableDictionary new];
    
    [attributes setObject:font forKey:NSFontAttributeName];
    [attributes setObject:textColor forKey:NSForegroundColorAttributeName];
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
{
    if (!self.buttonTips)
    {
        return nil;
    }
    NSString *text = self.buttonTips;
    UIFont *font = font = [UIFont systemFontOfSize:14];
    UIColor *textColor = [UIColor blueColor];
    NSMutableDictionary *attributes = [NSMutableDictionary new];
    
    [attributes setObject:font forKey:NSFontAttributeName];
    [attributes setObject:textColor forKey:NSForegroundColorAttributeName];
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    if(self.tipImg){
        return self.tipImg;
    }else{
        return [UIImage hb_imagePathWithName: @"no_data" bundle:@"XFDCommon" targetClass:NSClassFromString(@"HBBaseController")];
    }
}

- (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView
{
    return 16;
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView
{
    CGFloat top = [self.topMargin floatValue];
    return -top;
}

#pragma mark - DZNEmptyDataSetDelegate Methods
- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView
{
    return YES;
}

- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView
{
    return YES;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
{
    return YES;
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button
{
    if (self.buttonClickBlock) {
        self.buttonClickBlock();
    }
}


@end
